<script setup lang="ts">
import { ref } from 'vue'
import type { BannerItme, CategoryItem, HotItem } from '@/types/home'
import CustomNavbar from './components/CustomNavbar.vue'
import { getHomeBannerAPI, getHomeCategoryAPI, getHotPanleAPI } from '@/services/home'
import { onLoad } from '@dcloudio/uni-app'
import CategoryPanel from './components/CategoryPanel.vue'
import type { XtxGuessInstance } from '@/components/components'
import PageSkeleton from '@/components/PageSkeleton.vue'
// 猜你喜欢列表
const GuessRef = ref<XtxGuessInstance>()
const bannerList = ref<BannerItme[]>([])
const categoryList = ref<CategoryItem[]>([])
const HotPanelList = ref<HotItem[]>([])
const getBannerListData = async () => {
  const res = await getHomeBannerAPI()
  bannerList.value = res.result
  console.log(res)
}
// 前台分类数据获取
const getHomeCartegory = async () => {
  const res = await getHomeCategoryAPI()
  console.log(res)

  categoryList.value = res.result
}

// 获取热门数据
const getHotPanleData = async () => {
  const res = await getHotPanleAPI()
  console.log(res)
  HotPanelList.value = res.result
}
const onScrolltoLower = () => {
  GuessRef.value?.getMore()
}
// 控制下拉状态
const isTrigger = ref(false)
const onrefresherrefresh = async () => {
  isTrigger.value = true
  GuessRef.value?.resetData()
  await Promise.all([getBannerListData(), getHomeCartegory(), getHotPanleData()])
  console.log(isTrigger.value)

  isTrigger.value = false
  console.log(isTrigger.value)
}
const isLoading = ref(true)
onLoad(async () => {
  await Promise.all([getBannerListData(), getHomeCartegory(), getHotPanleData()])
  isLoading.value = false
})
import Hotpanel from './components/HotPanel.vue'
</script>
<template>
  <!-- 自定义导航栏 -->
  <CustomNavbar />
  <scroll-view
    scroll-y
    class="scroll-view"
    @scrolltolower="onScrolltoLower"
    refresher-enabled
    @refresherrefresh="onrefresherrefresh"
    :refresher-triggered="isTrigger"
  >
    <!-- 股价屏 -->
    <PageSkeleton v-if="isLoading"></PageSkeleton>
    <template v-else>
      <!-- 自定义轮播图 -->
      <XtxSwiper :list="bannerList" />
      <!-- 分类面板 -->
      <CategoryPanel :list="categoryList"></CategoryPanel>
      <!-- 热门组件 -->
      <Hotpanel :list="HotPanelList"></Hotpanel>
      <!-- 猜你喜欢 -->
      <XtxGuess ref="GuessRef"></XtxGuess>
    </template>
  </scroll-view>
</template>

<style lang="scss">
//
#app,
page {
  background-color: #f7f7f7;
  height: 100%;
  display: flex;
  flex-direction: column;
}
.scroll-view {
  flex: 1;
}
</style>
